<?php
/**
 * 
 * 核心类
 * @author Charles
 *
 */
class R extends lib_base_base {
	
	protected $action='';
	
	protected $module='';
	
	public function Before()
	{

	}
	
	/**
	 * 
	 * 入口方法
	 */
	public  function Run(){
		try{
			
			self::newsClass('config',new config());				  #加载配置文件	
			self::newsClass('requests',request::__Auto());		  #加载外部数据类
			self::newsClass('languages',getlan()); 				  #加载语言包	
			self::newsClass('db',lib_db_factory::get_instance()); #加载数据类
	    
			$this->bulid();
			$this->exec();
		}catch (Exception $e){
			$this->delError($e);
		}
	}
		
	/**
	 * 分析URL请求
	 */ 
	protected function bulid(){	

		$url=self::exurl();
		if(preg_match('/^[_a-zA-Z][_a-zA-Z0-9]+/',$url[1])){
			$this->action=$url[1];
			if(!isset($url[0]) && empty($url[0])){
				$this->module='index';
			}else{
				$this->module=$url[0];
			}
		}else{
			$this->ThewError('Can not have special characters');
		}
    
 	}
 	
 	/**
 	 * 路由功能函数
 	 */
 	protected function exec(){
 		
 	    if(R::getClass('config')->error){ //調試，跳轉
 	        $this->ThewError('error');
 	    }
 	    if(R::getClass('config')->perFile){  //後台權限判斷，還沒寫完成
    		$perClass=new menuPer();	
    		$perClass->selectPer();  	
 	    }
 	    
 	    $control='';	    
		self::implode(HZHIHU_ROOT.CONTROL_FILE.$control);
		self::implode(HZHIHU_ROOT.MODULE_FILE);
		
		$control_module='control_'.$this->module;
		self::newsClass($control_module);
		if(class_exists($control_module)){	   //判斷是否有此控制層
		    $control=new $control_module();	
			if(method_exists($control,'do'.$this->action)){   //調用控制層方法
				$method='do'.$this->action;
				$control->$method();
			}else{
				$this->ThewError(sprintf('Class in method not found %s','do'.$this->action));
			}
		}else{
		 $this->ThewError(sprintf('Class  not found %s',$control_module));
		}
 	}
 	
 	
	/**
	 * 要加载的文件,可直接传入一个目录,只读出此目录下文件,不递归
	 * @param string $dir 目录或者文件名
	 * @return bool 是否成功
	 */
 	public static  function implode($dir)
 	{	
 		/**
 		 * 检查是否有缓存
 		 */
 		$php_self=basename(HZHIHU_PHP_SELF,'.php');
 		if(lib_cache_file::excache('lib_class_'.$php_self))
 		{
 			$isAry=array('lib','config','helpers',CONTROL);		#基本目录，方便在基本目录实例类
	 		if(!is_file($dir)){
		 		if(!is_dir($dir)) return false;
		 		$d = dir($dir);
		 		while (false !== ($entry = $d->read())) {				#查找此目录下所有文件
		 			if($entry!='.' && $entry!='..' && is_file($dir.$entry))
		 			{
		 				if(substr_count($dir.$entry,HZHIHU_DIREND)>=2)	#如果此文件大于2级目录
		 				{
							$dirAry=explode(HZHIHU_DIREND,str_replace(HZHIHU_ROOT, "", $dir));
							$dirAry=array_filter($dirAry);		#去掉数组里的空值
		 					if(in_array($dirAry[0],$isAry)){	#在基本目录，则文件不需要带基本目录的名称
								array_shift($dirAry);
							}							
							$dirAry[]=basename ($entry,".php");#去掉文件名后的PHP
							$file=implode('_',$dirAry);
		 				}else{
		 					$file=basename ($entry,".php");
		 				}
		 				self::regeClass($file,$dir.$entry);
		 			}
		 		}
		 		$d->close();
	 		}else{
	 			$file = basename ($dir,".php");
	 			self::regeClass($file,$dir);
	 		}
	 		unset($dirAry,$file,$entry,$dir);
 		}else{
 			if(isset(self::$class))
 					return true;

 			$dirAry=lib_cache_file::readerCache('lib_class_'.$php_self);
 			foreach ($dirAry as $K=>$V)
 			{
 				self::regeClass($K,$V);
 			}
 			unset($dirAry);
 		}
 		return true;
 	}
 	
 	/**
 	 * require文件
 	 */
 	static  public function newsClass($class,$newclass='')
 	{
 		if(isset(self::$classObj[$class])) return true;
 		if(is_array(self::$class) && array_key_exists($class,self::$class))
 		{
 			if(is_file(self::$class[$class])){	
 				require_once self::$class[$class];
 			}		
 		}elseif(is_object($newclass)){
 			self::$classObj[$class]=$newclass;
 		}else{
 			self::$classObj[$class]=$newclass;
 		}
 		if(isset(self::$classObj[$class]))
 			return self::$classObj[$class];
 		else 
 			return false;
 		
 	}
 	
 	/**
 	 * 获得实例类
 	 */
 	static public function getClass($class='')
 	{
 		if(empty($class)) return self::$classObj;
 		if(is_array(self::$classObj)){
	 		if(array_key_exists($class,self::$classObj)){
	 			return self::$classObj[$class];
	 		}
 		}
 		return false;
 	}
 	
 	
 	/**
 	 * 
 	 * 自动加载类文件
 	 * @param string $class 类名
 	 */
 	static public function autoload($class)
 	{
 		if(isset(self::$classObj[$class])) return true; 	
 		if(is_array(self::$class) && array_key_exists($class,self::$class))
 		{
 			if(is_file(self::$class[$class])){	
 				require_once self::$class[$class];
 				return;
 			}	
 		}
 		
 		if(strpos($class,'_'))
 		{
 			$classAry=explode('_',$class);
 			$filename=array_pop($classAry);
 			include_once HZHIHU_ROOT.implode(HZHIHU_DIREND, $classAry).HZHIHU_DIREND.$filename.'.php';
 		}
 		
 	}
 	
 		
 	/**
 	 * 
 	 * 获取GET[do],分解成控制层和方法名
 	 * @return array array($do,$menth);
 	 */
    public static function exurl()
    {
    	$config=self::getClass('config');
    	$request=request::__Auto();
    	if($request){
	    	if(!isset($request->get['do'])) $request->get['do']=$config->App_index;
			if(strpos($request->get['do'],$config->Get_explode))	{
				$get=explode($config->Get_explode,$request->get['do']);
			}
			return array($get[0],$get[1]);  
    	}
    	return FALSE;  	
    }
    
    public function After()
    {
    	$php_self=basename(HZHIHU_PHP_SELF,'.php');
    	if(lib_cache_file::excache('lib_class_'.$php_self)){
    		lib_cache_file::createCache('lib_class_'.$php_self,self::$class);
    	}
    }
   
}

spl_autoload_register(array('R','autoload'));
